package com.demo.user;

import com.demo.common.model.FUser;
import com.demo.utils.Random;
import com.jfinal.core.Controller;

import java.util.List;

/**
 * 用户表
 */
public class UserController extends Controller {


    /**
     * 用户登录
     */
    public void UserLogin(String uname,String pwd){

        FUser first = new FUser().findFirst("select id,uname,pwd from f_user where uname = ? ",uname);
        if (first!=null){
            if (first.getPwd().equals(pwd)){
                first.setPwd("");
                setAttr("user",first);
                setAttr("msg","登录成功");
                setAttr("data","200");
                renderJson();
            }else {
                setAttr("msg","密码错误");
                setAttr("data","300");
                renderJson();
            }
        }else {
            setAttr("msg","用户名不存在");
            setAttr("data","300");
            renderJson();
        }
    }
    /**
     * 用戶注册
     */
    public void Register(String uname,String pwd){

        FUser fUsers = new FUser().findFirst("select id,uname from f_user where uname = ?", uname);
        if (fUsers!=null){
            setAttr("msg","用户名已存在");
            setAttr("data","300");
            renderJson();
        }else {
            FUser user = new FUser();
            user.setPwd(pwd);
            user.setUname(uname);
            user.setId(Random.generateString(10));
            boolean save = user.save();
            if (save) {
                setAttr("user",user);
                setAttr("msg","注册成功");
                setAttr("data","200");
                renderJson();
            }else {
                setAttr("msg","注册失败");
                setAttr("data","300");
                renderJson();
            }
        }
    }
}